package cuongnh.project.japanesediary.vocabulary.ui.wizard;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import cuongnh.project.japanesediary.vocabulary.ui.wizard.page.NewCategoryWizardPage;

@Component("newCategoryWizard")
@Scope("prototype")
public class AddCategoryWizard extends Wizard implements INewWizard{

	private Category category;
	private NewCategoryWizardPage page;
	@Autowired
	private CommonServices commonServices;
	private IWorkbench workbench;
	
	public AddCategoryWizard() {
		setWindowTitle("Create/Edit a new Category wizard");
		this.category = new Category();
	}

	public AddCategoryWizard(Category category,
			CommonServices commonServices) {
		setWindowTitle("Create/Edit a new Category wizard");
		this.category = category;
		this.commonServices = commonServices;
	}

	@Override
	public void addPages() {
		page = new NewCategoryWizardPage(category,
				commonServices);
		addPage(page);
	}

	@Override
	public boolean performFinish() {
		category.setParentCategory(page.getParentCategory());
		commonServices.getCategoryDao().saveOrUpdate(category);
		return true;
	}
	
	public Category getCategory(){
		return category;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbench = workbench;		
	}

}
